/**
 * 
 */
package sailing.worker;

import java.util.concurrent.CountDownLatch;

import javax.swing.SwingWorker;

import cjlite.logger.Logger;

import sailing.ui.progressdialog.ProgressDialog;

/**
 * @author kevin
 * 
 */
public abstract class FileIOWorker<T, V> extends SwingWorker<T, V> {
	private static Logger logger = Logger.thisClass();

	private ProgressDialog progressDialog;
	private boolean executed = false;

	private CountDownLatch cdl = new CountDownLatch(1);

	private int progress;

	public abstract String getWorkerMsg();

	public void setProgressDialog(ProgressDialog progressDialog) {
		this.progressDialog = progressDialog;
	}

	protected void appendTaskMsg(String msg) {
		this.progressDialog.appendTaskMsg(msg);
	}

	protected void updateProgress(int progress) {
		this.progress = progress;
		this.progressDialog.setProgress(progress);
	}

	protected void incresProgress(int incres) {
		this.progress += incres;
		this.progressDialog.setProgress(progress);
	}

	protected void appendTaskMsg(int progress, String msg) {
		updateProgress(progress);
		appendTaskMsg(msg);
	}

	protected void countDown() {
		cdl.countDown();
	}

	public void await() {
		try {
			logger.trace("aaaaaaaaaa wait....................");
			cdl.await();
		} catch (InterruptedException e) {
		}
	}

	public abstract String getTerminationMsg();

	public void terminate() {
		this.countDown();
		this.done();
	}

}
